package threadDemo;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author macpro
 * @date 2019-07-14
 * @function
 */
public class ReentrantLockInterrupt {
    public static void main(String[] args) {
        try {
            Thread t1 = new ReentrantLockInterrupt.MyThread("t1");  // 新建“线程t1”
            System.out.println(t1.getName() +" ("+t1.getState()+") is new.");

            t1.start();                      // 启动“线程t1”
            System.out.println(t1.getName() +" ("+t1.getState()+") is started.");

            // 主线程休眠300ms，然后主线程给t1发“中断”指令。
            Thread.sleep(300);
            t1.interrupt();
            System.out.println(t1.getName() +" ("+t1.getState()+") is interrupted.");

            // 主线程休眠300ms，然后查看t1的状态。
            Thread.sleep(300);
            System.out.println(t1.getName() +" ("+t1.getState()+") is interrupted now.");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        ReentrantLock lock = new ReentrantLock();

        new Thread(new Runnable() {
            @Override
            public void run() {

                lock.lock();
                try {
                    System.out.println(System.currentTimeMillis() +  " while ture");
                    while (true){

                    }
                }catch (Exception e){
                    e.printStackTrace();
                }finally {
                    lock.unlock();
                    System.out.println(System.currentTimeMillis()+ "  finally  ");
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(100);
                    System.out.println(System.currentTimeMillis() +" start");
                    lock.lockInterruptibly();
                    if (lock.tryLock(3, TimeUnit.SECONDS)) {
                        //TODO
                        System.out.println(System.currentTimeMillis() +" start 33");
                    }else{
                        //超时直接中断优先级低的Task
                        Thread.currentThread().interrupt();
                        lock.lock();
                        System.out.println(System.currentTimeMillis() +" start do something");
                        //TODO
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {

                }
            }
        }).start();
    }

    static class MyThread extends Thread {

        public MyThread(String name) {
            super(name);
        }

        @Override
        public void run() {
            try {
                int i=0;
                while (!isInterrupted()) {
                    Thread.sleep(100); // 休眠100ms
                    i++;
                    System.out.println(Thread.currentThread().getName()+" ("+this.getState()+") loop " + i);
                }
            } catch (InterruptedException e) {
                System.out.println(Thread.currentThread().getName() +" ("+this.getState()+") catch InterruptedException.");
            }
        }
    }

}

